package com.utils.movieClipToBitmap
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.utils.clearInterval;
	import flash.utils.setInterval;
	/**
	 * 模拟影片剪辑播放 
	 * @author Admin
	 */	
	public class BitmapClip extends Bitmap
	{
		private static var _resourceBox:Object;//资源池
		private var _bmdList:Array;
		private var _interval:int;
		private var _curIndex:uint = 0;
		private var _dealy:uint = 40;
		private var _totalFrames:uint;
		
		/**
		 * 位图剪辑 模拟影片剪辑
		 * @param bitmapData
		 * @param pixelSnapping
		 * @param smoothing
		 * @param swfName swf文件名
		 * @param dealy 25 frame/s
		 */		
		public function BitmapClip(swfName:String,dealy:uint = 40,bitmapData:BitmapData=null, pixelSnapping:String="auto", smoothing:Boolean=false)
		{
			super(bitmapData, pixelSnapping, smoothing);
			if(!_resourceBox) throw Error('资源池尚未赋值!');
			_bmdList = _resourceBox[swfName];
			if(!_bmdList) throw Error('资源池内无' + name +'数据!');
			play();//播放
			_totalFrames = _bmdList.length;
		}
		/**
		 * 从第几帧开始播放
		 * @param frame 从1开始
		 */		
		public function gotoAndPlay(frame:uint):void
		{
			if(frame<1) frame=1;
			_curIndex = frame-1;
			play();
		}
		
		/**
		 * 从第几帧开始播放
		 * @param frame 从1开始
		 */		
		public function gotoAndStop(frame:uint):void
		{
			if(frame<1) frame=1;
			_curIndex = frame-1;
			stop();
			this.bitmapData = _bmdList[_curIndex % _bmdList.length].clone();
		}
		
		/**播放*/		
		public function play():void{
			clearInterval(_interval);
			_interval = setInterval(changeBitmapData,_dealy);
		}
		
		/**停止*/		
		public function stop():void{
			clearInterval(_interval);
			--_curIndex;
			changeBitmapData();
		}
		
		/**下一帧*/		
		public function nextFrame():void{
			stop();
			changeBitmapData();
		}
		
		/**前一帧*/	
		public function prevFrame():void{
			stop();
			--_curIndex;
			if(_curIndex <= 0) _curIndex=0;
			this.bitmapData = _bmdList[_curIndex % _bmdList.length].clone();
		}
		
		/**替换数据 */		
		private function changeBitmapData():void
		{
			++_curIndex;
			this.bitmapData = _bmdList[_curIndex % _bmdList.length].clone();
		}
		
		
		
		
		
		/**当前帧*/		
		public function get curentFrame():uint{return _curIndex+1;}
		
		/**总帧数*/		
		public function get totalFrames():uint{return _totalFrames;}
		
		/**添加资源池数据 */		
		public static function set resourceBox(value:Object):void{_resourceBox = value;}

		override public function get width():Number
		{
			return _bmdList[_curIndex % _bmdList.length].clone().width;
		}
		
		override public function get height():Number
		{
			return _bmdList[_curIndex % _bmdList.length].clone().height;
		}
	}
}